<?php 

class Store_model extends CI_Model{
	public function __construct() {
        parent::__construct();
		$this->load->library('upload');
    }
	
	function create($data){
		$config = $this->upload->data('logo');
		$data = array(
					'nama_toko' => $this->input->post('nama_toko'),
					'jenis_toko' =>  $this->input->post('jenis_toko'),
					'logo' => ''.$config['file_name'],
					'description' => $this->input->post('description'),
					'telepon' => $this->input->post('telepon'),
					'hp' => $this->input->post('hp'),
					'alamat' => $this->input->post('alamat'),
					'kota' => $this->input->post('kota'),
					'provinsi' => $this->input->post('provinsi'),
					'kode_pos' => $this->input->post('kode_pos'),
					'negara' => $this->input->post('negara'),
					'status' => $this->input->post('status')
				);
		$this->db_front->insert('store',$data);
	}
	
	function getAll($page='', $limit=''){
		if(!empty($limit))
		$this->db->limit($limit,$page);
		
		$query = $this->db_front->get('store');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_toko_by_id($toko_id){
		$this->db_front->where('toko_id', $toko_id);
		$this->db_front->from('store');
		$query = $this->db_front->get();
		return $query->row_array();		
		}
	
	function update($toko_id){
		$config = $this->upload->data('logo');
		if(!empty($config['file_name'])){
			$data = array(
					'nama_toko' => $this->input->post('nama_toko'),
					'jenis_toko' =>  $this->input->post('jenis_toko'),
					'logo' => ''.$config['file_name'],
					'description' => $this->input->post('description'),
					'telepon' => $this->input->post('telepon'),
					'hp' => $this->input->post('hp'),
					'alamat' => $this->input->post('alamat'),
					'kota' => $this->input->post('kota'),
					'provinsi' => $this->input->post('provinsi'),
					'kode_pos' => $this->input->post('kode_pos'),
					'negara' => $this->input->post('negara'),
					'status' => $this->input->post('status')
				);
        $this->db_front->where('toko_id', $toko_id);
        $this->db_front->update('store', $data);
		}else{
			$data = array(
					'nama_toko' => $this->input->post('nama_toko'),
					'jenis_toko' =>  $this->input->post('jenis_toko'),
					//'logo' => $this->input->post('logo'),
					'description' => $this->input->post('description'),
					'telepon' => $this->input->post('telepon'),
					'hp' => $this->input->post('hp'),
					'alamat' => $this->input->post('alamat'),
					'kota' => $this->input->post('kota'),
					'provinsi' => $this->input->post('provinsi'),
					'kode_pos' => $this->input->post('kode_pos'),
					'negara' => $this->input->post('negara'),
					'status' => $this->input->post('status')
				);
        $this->db_front->where('toko_id', $toko_id);
        $this->db_front->update('store', $data);
		}
		
    }
	
	function select($toko_id){
        return $this->db_front->get_where('store', array('toko_id'=>$toko_id))->row();
    }
	
	function delete($toko_id){
        $this->db_front->delete('store', array('toko_id'=>$toko_id));
    }
}
?>